﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iMapa.Backend;
using iMapa.UI.ModelView.Main;
using iMapa.UI.ModelView.Common;
using System.Drawing;
using System.Windows.Shapes;
using iMapa.Utils;
using System.Windows.Data;
using System.Windows.Controls;
using iMapa.UI.View.Common;
using iMapa.Helper;

namespace iMapa.UI.ModelView.Editor
{
    public class InteractObjectViewModel:ViewModelBase
    {
        private InteractObject iObject;
        private MainViewModel root;
        private MapViewModel parent;
        private ActionTypeViewModel actionType;

        public InteractObjectViewModel(MainViewModel root,InteractObject iObject, MapViewModel parent)
        {
            this.root = root;
            this.iObject = iObject;
            this.parent = parent;
        }

        public InteractObject IObject
        {
            get { return iObject; }
            set { iObject = value; }
        }

        public string Name
        {
            get { return iObject.Name; }
            set
            {
                iObject.Name = value;
                onPropertyChanged("Name");
            }
        }

        public int X
        {
            get { return (int)((parent.DisplaySize.Width /parent.Image.Width)* iObject.Position.X); }
            set
            {
                iObject.Position = new Point( (int)((parent.Image.Width/parent.DisplaySize.Width)*value), iObject.Position.Y);
                onPropertyChanged("X");
                onPropertyChanged("Xreal");
            }
        }

        public int Y
        {
            get { return (int)((parent.DisplaySize.Height / parent.Image.Height) * iObject.Position.Y); }
            set
            {
                iObject.Position = new Point(iObject.Position.X, (int)((parent.Image.Height / parent.DisplaySize.Height) * value));
                onPropertyChanged("Y");
                onPropertyChanged("Yreal");
            }
        }
        public int Xreal
        {
            get { return iObject.Position.X; }
        }

        public int Yreal
        {
            get { return iObject.Position.Y; }
        }

        public ActionTypeViewModel ActionType
        {
            get
            {
                if (actionType == null && iObject.ActionType != null)
                {
                    foreach (ActionTypeViewModel vm in parent.Plugins)
                    {
                        if (vm.Type.Equals(iObject.ActionType))
                        {
                            actionType = vm;
                            break;
                        }
                    }
                }
                return actionType;
            }
            set
            {
                if (iObject.ActionType != value.Type)
                {
                    iObject.ActionParams = null;
                }
                actionType = value;
                iObject.ActionType = value.Type;
                onPropertyChanged("ActionType");
            }
        }

        public int Offset
        {
            get { return -RadiusShow / 2; }
        }
        public int TextOffset
        {
            get { return RadiusShow / 2; }
        }

        public int Radius
        {
            get { return iObject.Radius; }
            set
            {
                iObject.Radius = value;
                onPropertyChanged("Radius");
                onPropertyChanged("RadiusShow");
                onPropertyChanged("Offset");
                onPropertyChanged("TextOffset");
            }
        }

        public int RadiusShow
        {
            get{return (int)((parent.DisplaySize.Height / parent.Image.Height) * iObject.Radius);} 
        }

        public MapViewModel Parent
        {
            get { return parent; }
            set { parent = value; }
        }
        
        public Boolean IsSelected
        {
            get { return parent.SelectedIObj == this; }
        }

        public InteractPoint CreatePoint()
        {
            InteractPoint p= new InteractPoint(this);
            return p;
        }
    }
}
